# =============================================================================
# April 21 2025
# R code used to produce all results, tables, and figures in the SI Online Appendix A
# Rebecca Cordell
# Unpacking the Role of In-Group Bias in US Public Opinion on Human Rights Violations
# American Journal of Political Science
# https://doi.org/10.7910/DVN/TGAL7M
# =============================================================================

# Clear work environment
rm(list=ls())

# Install Packages
#install.packages("dplyr")
#install.packages("cjpowR")

# Required Packages
library("dplyr")
library("cjpowR")

options(scipen = 999)
options(warn=-1)

# -----------------------------------------------------------------------------
# Read in data
# -----------------------------------------------------------------------------

hr_survey<-read.csv("cordell_ingroupbiashumanrights_data.csv", header=TRUE, stringsAsFactors = FALSE)

# =============================================================================
# Table A.1: Power Analysis for Average Marginal Component Effects (AMCEs)
# =============================================================================

# -----------------------------------------------------------------------------
# Calculate required sample size for main effects
# -----------------------------------------------------------------------------

# Create AMCE values
amce_vals <- c(0.01, 0.02, 0.03, 0.05)

# Create function to calculate minimum sample sizes
taba1 <- lapply(amce_vals, function(a) {
  cjpowr_amce(amce = a, power = 0.8, levels = 5, alpha = 0.05)
}) %>%
  dplyr::bind_rows() %>%
  mutate(
    respondents = round(n / 2 / 5, 0),
    observations = round(n, 0)
  ) %>%
  select(respondents, observations, amce, power, alpha, levels)

# -----------------------------------------------------------------------------
# Create table
# -----------------------------------------------------------------------------

# Print Table A.1
write.csv(taba1, "tab_a1.csv", row.names=FALSE)

# =============================================================================
# Table A.2: Power Analysis for Average Component Interaction Effects (ACIEs)
# =============================================================================

# -----------------------------------------------------------------------------
# Calculate required sample size for conditional effects
# -----------------------------------------------------------------------------

# Create ACIE values
delta3_vals <- c(0.01, 0.02, 0.03, 0.05)

# Create function to calculate minimum sample sizes
taba2 <- lapply(delta3_vals, function(d3) {
  out <- cjpowr_amcie(
    delta0 = 0.5,
    delta1 = 0,
    delta3 = d3,
    alpha = 0.05,
    power = 0.8,
    levels1 = 2,
    levels2 = 5
  )
  out$acie <- d3
  return(out)
}) %>%
  dplyr::bind_rows() %>%
  mutate(
    respondents = round(n / 2 / 5, 0),
    observations = round(n, 0)
  ) %>%
  select(respondents, observations, acie, power, alpha, levels1, levels2)

# -----------------------------------------------------------------------------
# Create table
# -----------------------------------------------------------------------------

# Print Table A.2
write.csv(taba2, "tab_a2.csv", row.names=FALSE)

# -----------------------------------------------------------------------------
# In-text Results
# -----------------------------------------------------------------------------

# "The results suggested that the survey experiment required approximately 3,200 respondents in order to have a well powered design, with each participant assessing 2 profiles per task and repeating this task 5 times (32,000 observations in total)."
# 3,200 respondents
ceiling(taba2$respondents[4]/100)*100
# 32,000 observations
ceiling(taba2$observations[4]/1000)*1000